package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

public class LoadCallNoTransfer extends AbstractTransfer{
    public LoadCallNoTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String cardNo = (String) params.get("cardNo");
        Calendar calendar = Calendar.getInstance();
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("DataNode1").addElement("DataRow");
        dataRowElement.addAttribute("ID_NO", cardNo);
        return tplDoc.asXML();
    }
    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (dataRows != null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String, Object> dataMap = new HashMap<>();
                    LoadCallNoTransfer.LoadCallNoInnerMessage dataRowMessage = new LoadCallNoTransfer.LoadCallNoInnerMessage(dataRow);
                    dataMap.put("deptCode", dataRowMessage.getDeptCode());
                    dataMap.put("docName", dataRowMessage.getDocName());
                    dataMap.put("ampm", dataRowMessage.getAmpm());
                    dataMap.put("no", dataRowMessage.getNo());
                    dataMap.put("curNo", dataRowMessage.getCurNo());
                    dataMap.put("beforeNum", dataRowMessage.getBeforeNum());
                    dataMap.put("visitDate", dataRowMessage.getVisitDate());
                    dataMap.put("state", dataRowMessage.getState());
                    dataList.add(dataMap);
                }
            }
            resMap.put("list",dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);


        }
        return resMap;
    }

    private class LoadCallNoInnerMessage implements InnerMessage {
        /**
         * 科室名称
         */
        private String deptCode;
        /**
         * 医生名称
         */
        private String docName;
        /**
         * 时段
         */
        private String ampm;
        /**
         * 挂号序号
         */
        private String no;
        /**
         * 当前序号
         */
        private String curNo;
        /**
         * 前面排队人数
         */
        private String beforeNum;
        /**
         * 就诊日期
         */
        private String visitDate;
        /**
         * 当前状态
         */
        private String state;

        public LoadCallNoInnerMessage(Element element){
            this.deptCode = element.attributeValue("DEPT_CODE");
            this.docName = element.attributeValue("DOCTOR");
            this.ampm = element.attributeValue("TIME_DESC");
            this.no = element.attributeValue("REGIST_ORDER");
            this.curNo = element.attributeValue("CURRENT_NO");
            this.beforeNum = element.attributeValue("LEFT_NO");
            this.visitDate = element.attributeValue("CLINIC_DATE");
            this.state = element.attributeValue("STATE");
        }

        public String getDeptCode() { return deptCode; }

        public String getDocName() { return docName; }

        public String getAmpm() { return ampm; }

        public String getNo() { return no; }

        public String getCurNo() { return curNo; }

        public String getBeforeNum() { return beforeNum; }

        public String getVisitDate() { return visitDate; }

        public String getState() { return state; }
    }

}